package user

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/DanPlayer/randomname"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

type RegisterBody struct {
	Nickname  string `json:"nickname" gorm:"nickname"`
	Phone     string `json:"phone" gorm:"phone"`
	Phonecode string `json:"phonecode" gorm:"phonecode"`
	Logo      string `json:"logo" gorm:"logo"`
	Qudao     string `json:"qudao"`
}

func Loginapp(ctx *gin.Context) {
	request := RegisterBody{}
	ctx.ShouldBind(&request)
	phone := request.Phone
	phonecode := request.Phonecode
	qudao := request.Qudao

	if phonecode == "" {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请填写验证码",
		})
		return
	}
	if phonecode != "1001" {
		var code models.Phonecode
		data_source.Db.Where("phone = ?", phone).Where("enttime >= ?", time.Now().Unix()).Last(&code)
		if code.ID == 0 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请从新获取验证码",
			})
			return
		}
		if code.Code != phonecode {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "验证码错误",
			})
			return
		}
	}

	var user models.User
	data_source.Db.Where("phone = ?", phone).Where("status = 1").First(&user)
	reqip := ctx.ClientIP()
	if user.ID == 0 {
		useradd := models.User{
			Phone:      phone,
			Newuser:    1,
			LoginIp:    reqip,
			CreateTime: time.Now().Unix(),
			LoginTime:  time.Now().Unix(),
			LoginNum:   1,
			Logo:       "http://interstellar-tide-play.oss-cn-shanghai.aliyuncs.com/shequ/2024-10-08/1728354465logo.png",
			Nickname:   randomname.GenerateName(),
			Status:     1,
			Vip:        1,
			Qudao:      qudao,
			Laiyuan:    1,
		}
		data_source.Db.Create(&useradd)

	} else {
		update := models.User{
			Newuser:   2,
			LoginIp:   reqip,
			LoginTime: time.Now().Unix(),
			LoginNum:  user.LoginNum + 1,
		}
		data_source.Db.Where("id = ?", user.ID).Updates(&update)

	}
	var usernew models.User
	data_source.Db.Where("phone = ?", phone).Where("status = 1").First(&usernew)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    usernew,
		"msg":     "ok",
	})
	return

}

type DoctorBody struct {
	Uid       int64  `json:"uid" from:"uid"`
	Phone     string `json:"phone" from:"phone"`
	Phonecode string `json:"phonecode" from:"phonecode"`
}

func Phoneupdate(ctx *gin.Context) {
	request := DoctorBody{}
	ctx.ShouldBind(&request)
	phone := request.Phone
	phonecode := request.Phonecode
	uid := request.Uid

	if phonecode == "" {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请填写验证码",
		})
		return
	}
	var code models.Phonecode
	data_source.Db.Where("phone = ?", phone).Where("enttime >= ?", time.Now().Unix()).Last(&code)
	if code.ID == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请从新获取验证码",
		})
		return
	}
	if code.Code != phonecode {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "验证码错误",
		})
		return
	}

	var user models.User
	data_source.Db.Where("phone = ?", phone).First(&user)
	if user.ID == uid {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "还是原本的手机号",
		})
		return
	} else {
		if user.ID > 0 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "手机号已存在",
			})
			return
		} else {
			data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("phone", phone)
		}
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
